package net.starwifi.wx.api;

import net.starwifi.wx.tools.HttpUtils;
import net.starwifi.wx.tools.StringUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 用户API
 * Created by ruoo on 16/1/26.
 */
public class UserAPI {



    /*
     * (用户信息)获取网页授权API
     * @param appId 公众号唯一标识
     * @param redirectUri 授权后重定向的回调链接地址，请使用urlencode对链接进行处理
     * @param responseType 返回类型
     * @param scope 作用域
     * @param state 自定义透传字段(1-128byte)
     * @return
    * */
    public static String getUserAuthUrl(String appId,String redirectUri,String responseType,String scope,String state){
        String baseUrl="https://open.weixin.qq.com/connect/oauth2/authorize";
        Map<String,String> params=new LinkedHashMap<String, String>();
        params.put("appid",appId);
        params.put("redirect_uri",redirectUri);
        params.put("response_type",responseType);
        params.put("scope",scope);
        params.put("state",state);
        return StringUtils.makeURL(baseUrl,params)+"#wechat_redirect";
    }


    /*
     * (用户信息)获取网页授权API
     * @param appId 公众号唯一标识
     * @param secret 公众号的appsecret
     * @param code 用户同意授权之后的code
     * @param grantType 填写为authorization_code
     * @return
     * <p>success result</p>
     * <pre>
     *     {
     *         "access_token":"ACCESS_TOKEN",
     *         "expires_in":7200,
     *         "refresh_token":"REFRESH_TOKEN",
     *         "openid":"OPENID",
     *         "scope":"SCOPE",
     *          "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
     *       }
     * </pre>
     */
    public static String getUserAccessToken(String appId,String secret,String code){
        String baseUrl="https://api.weixin.qq.com/sns/oauth2/access_token";
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("appid",appId);
        params.put("secret",secret);
        params.put("code",code);
        params.put("grant_type","authorization_code");
        return HttpUtils.get(baseUrl,params);
    }


}
